# Archlive <http://archlive-pkg.googlecode.com>

pkgbase="kernel3-cjk"
pkgname=('kernel3-cjk' 'kernel3-headers-cjk' 'linux-api-headers' 
	#'kernel3-tools'
	#'kernel3-docs'
	)
_realpkgname=kernel3
_basekernel=3.0.0
pkgver=3.0.0_rc6
pkgrel=1
arch=(i686 x86_64)
license=('GPL2')
url="http://www.kernel.org"
options=(!strip)
_configure_as_ARCH_way=y
_kernelname=${pkgname#kernel3}

# 是否保存编译目录
_keep_build_dir=y
## More special selection as below items.
#
makedepends=('git' 'gcc' libnewt elfutils 
	# 'xmlto' 'docbook-xsl'
	)
source=(kernel3.preset config config.x86_64 
	http://www.kernel.org/pub/linux/kernel/v3.0/testing/linux-3.0-rc6.tar.bz2
	## Gentoo patches
	4200_fbcondecor-0.9.6.patch
	#http://sources.gentoo.org/viewcvs.py/*checkout*/linux-patches/genpatches-2.6/trunk/2.6.31/4100_dm-bbr.patch
	#http://sources.gentoo.org/cgi-bin/viewvc.cgi/linux-patches/genpatches-2.6/trunk/3.0/4200_fbcondecor-0.9.6.patch
	#http://sources.gentoo.org/viewcvs.py/*checkout*/linux-patches/genpatches-2.6/trunk/2.6.31/4400_alpha-sysctl-uac.patch
	### Chinese Characters Patches 孙海勇 大侠的 内核中文补丁  http://youbest.cublog.cn/
	utf8-kernel-2.6.31-fbcondecor-1.patch.bz2
	utf8-kernel-2.6-fonts-3.patch.bz2
	utf8-kernel-3.0-core-1.patch
	### aufs2.1 patch
	aufs2-kbuild.patch
	aufs2-base.patch
	aufs2-standalone-3.0.patch
	aufs2.1_main.patch
)

md5sums=()
_build_dir="$srcdir/linux-3.0-rc6"

extrapatch() {
	cd $_build_dir
	msg2 "Add Gentoo patches 加入Gentoo官方基本补丁..."
	for i in $(ls $srcdir/4[0-9][0-9][0-9]*);do
    		echo "Applying $(basename $i)" 
  		patch -Np1 -i $i || return 1
	done
	# 4200_fbcondecor-0.9.6.patch

	msg2 "Add the kernel Chinese characters patches 加入孙海勇 大侠的 内核中文补丁  http://youbest.cublog.cn/"
	patch -Np1 -i $srcdir/utf8-kernel-2.6.31-fbcondecor-1.patch || return 1
	patch -Np1 -i $srcdir/utf8-kernel-3.0-core-1.patch || return 1
	patch -Np1 -i $srcdir/utf8-kernel-2.6-fonts-3.patch || return 1

	msg2 "Adding aufs2.1 standalone patch"
	patch -Np1 -i $srcdir/aufs2-kbuild.patch || return 1
	patch -Np1 -i $srcdir/aufs2-base.patch || return 1
	patch -Np1 -i $srcdir/aufs2-standalone-3.0.patch || return 1
	patch -Np1 -i $srcdir/aufs2.1_main.patch || return 1
}

build () {

	msg "Adding patches"
	extrapatch

	msg "Building kernel and modules..."

	cd "$_build_dir"
	make ${MAKEFLAGS} bzImage modules

	# Compile for kernel3-tools
	#cd tools/perf
	#sed -ri 's/python-config/python2-config/' Makefile
	#make DESTDIR="${pkgdir}/usr" all man

	msg "Compile finished!"
}

package_kernel3-cjk(){
	pkgdesc="The Linux Kernel and modules, with UNIVT patches"
	groups=('base' 'CJK')
	provides=("kernel26=$_basekernel" "kernel26=$_basekernel-ARCH" 'kernel3' 'aufs2' 'nouveau-drm')
	conflicts=('kernel24' 'kernel24-scsi' 'kernel26-scsi')
	depends=('coreutils' 'module-init-tools>=3.12-2' 'mkinitcpio>=0.6.8')
	optdepends=('crda: to set the correct wireless channels of your country')

	replaces=('kernel24' 'kernel24-scsi' 'kernel26-scsi'
            'alsa-driver' 'ieee80211' 'hostap-driver26'
            'pwc' 'nforce' 'squashfs' 'unionfs' 'ivtv'
            'zd1211' 'kvm-modules' 'iwlwifi' 'rt2x00-cvs'
            'gspcav1' 'atl2' 'wlan-ng26' 'rt2500' 'nouveau-drm')
 	backup=(etc/mkinitcpio.d/${pkgname}.preset)
	install=kernel3.install

	KARCH="x86"
	cd "$_build_dir"
	_kernver="$(make kernelrelease)"

	mkdir -p ${pkgdir}/{lib/modules,lib/firmware,boot}

	msg "Installing kernel modules..."
	make INSTALL_MOD_PATH="$pkgdir" modules_install

	msg "Installing kernel image..."
	install -Dm644 System.map  "$pkgdir/boot/System.map3${_kernelname}"
	install -Dm644 arch/$KARCH/boot/bzImage "$pkgdir/boot/vmlinuz3${_kernelname}"
	install -Dm644 .config  "$pkgdir/boot/kconfig3"

	#msg "add vmlinux"
	#install -m644 -D vmlinux "${pkgdir}/usr/src/linux-${_kernver}/vmlinux"

	msg "Installing preset file..."
	install -Dm644 "$srcdir/kernel3.preset" \
		"$pkgdir/etc/mkinitcpio.d/${pkgname}.preset" || return 1
	sed \
		-e "s|source .*|source /etc/mkinitcpio.d/kernel3${_kernelname}.kver|g" \
		-e "s|default_image=.*|default_image=\"/boot/${pkgname}.img\"|g" \
		-e "s|fallback_image=.*|fallback_image=\"/boot/${pkgname}-fallback.img\"|g" \
		-i ${pkgdir}/etc/mkinitcpio.d/${pkgname}.preset

	msg "Update version on install file"
	sed \
		-e  "s/KERNEL_NAME=.*/KERNEL_NAME=${_kernelname}/g" \
		-e  "s/KERNEL_VERSION=.*/KERNEL_VERSION=${_kernver}/g" \
		-i $startdir/kernel3.install

	msg "Generating kernel3.kver..."
	echo -e "# DO NOT EDIT THIS FILE\nALL_kver='$_kernver'" \
		> "$pkgdir/etc/mkinitcpio.d/kernel3${_kernelname}.kver" || return 1

	msg "remove build and source links"
	rm -f ${pkgdir}/lib/modules/${_kernver}/{source,build}

	msg "Remove the firmware..."
	rm -rf "$pkgdir/lib/firmware"

	msg "gzip -9 all modules to safe 100MB of space"
	find "$pkgdir" -name '*.ko' -exec gzip -9 {} \;

	msg "Kernel and modules installing finished."
}

package_kernel3-headers-cjk() {
	pkgdesc="Header files and scripts for building modules for kernel3"
	provides=("kernel26-headers=${pkgver}"  "kernel26-headers-git" "kernel3-headers=${pkgver}" )
	groups=('base' 'CJK')
	mkdir -p ${pkgdir}/lib/modules/${_kernver}
	cd ${pkgdir}/lib/modules/${_kernver}
	ln -sf ../../../usr/src/linux-${_kernver} build

	cd "$_build_dir"
	install -D -m644 Makefile \
		${pkgdir}/usr/src/linux-${_kernver}/Makefile
	install -D -m644 kernel/Makefile \
		${pkgdir}/usr/src/linux-${_kernver}/kernel/Makefile
	install -D -m644 .config \
		${pkgdir}/usr/src/linux-${_kernver}/.config
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/include

	for i in acpi asm-generic config crypto drm generated linux math-emu \
		media net pcmcia scsi sound trace video xen; do
		cp -a include/$i ${pkgdir}/usr/src/linux-${_kernver}/include/
	done

	msg "copy arch includes for external modules"
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/arch/x86
	cp -a arch/x86/include ${pkgdir}/usr/src/linux-${_kernver}/arch/x86/

	msg "copy files necessary for later builds, like nvidia and vmware"
	cp Module.symvers ${pkgdir}/usr/src/linux-${_kernver}
	cp -a scripts ${pkgdir}/usr/src/linux-${_kernver}

	msg "fix permissions on scripts dir"
	chmod og-w -R ${pkgdir}/usr/src/linux-${_kernver}/scripts
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/.tmp_versions

	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/arch/$KARCH/kernel

	cp arch/$KARCH/Makefile ${pkgdir}/usr/src/linux-${_kernver}/arch/$KARCH/
	if [ "$CARCH" = "i686" ]; then
		cp arch/$KARCH/Makefile_32.cpu ${pkgdir}/usr/src/linux-${_kernver}/arch/$KARCH/
	fi
	cp arch/$KARCH/kernel/asm-offsets.s ${pkgdir}/usr/src/linux-${_kernver}/arch/$KARCH/kernel/

	msg "add headers for lirc package"
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/video
	cp drivers/media/video/*.h  ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/video/
	for i in bt8xx cpia2 cx25840 cx88 em28xx et61x251 pwc saa7134 sn9c102 usbvision; do
		mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/video/$i
		cp -a drivers/media/video/$i/*.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/video/$i
	done
	#mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/staging/usbvideo/
	#cp -a drivers/staging/usbvideo/*.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/staging/usbvideo/

	msg "add docbook makefile"
	install -D -m644 Documentation/DocBook/Makefile \
		${pkgdir}/usr/src/linux-${_kernver}/Documentation/DocBook/Makefile
	msg "add dm headers"
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/md
	cp drivers/md/*.h  ${pkgdir}/usr/src/linux-${_kernver}/drivers/md
	
	msg "add inotify.h"
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/include/linux
	cp include/linux/inotify.h ${pkgdir}/usr/src/linux-${_kernver}/include/linux/

	msg "add wireless headers"
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/net/mac80211/
	cp net/mac80211/*.h ${pkgdir}/usr/src/linux-${_kernver}/net/mac80211/

	msg "add dvb headers for external modules"
	# in reference to:
	# http://bugs.archlinux.org/task/9912
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/dvb-core
	cp drivers/media/dvb/dvb-core/*.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/dvb-core/
	
	msg "add dvb headers for external modules"
	# in reference to:
	# http://bugs.archlinux.org/task/11194
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/include/config/dvb/
	cp include/config/dvb/*.h ${pkgdir}/usr/src/linux-${_kernver}/include/config/dvb/
	
	msg "add dvb headers"
	# for http://mcentral.de/hg/~mrec/em28xx-new"
	# in reference to:
	# http://bugs.archlinux.org/task/13146
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/frontends/
	cp drivers/media/dvb/frontends/lgdt330x.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/frontends/
	cp drivers/media/video/msp3400-driver.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/frontends/
	# add dvb headers  
	# in reference to:
	# http://bugs.archlinux.org/task/20402
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/dvb-usb
	cp drivers/media/dvb/dvb-usb/*.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/dvb-usb/
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/frontends
	cp drivers/media/dvb/frontends/*.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/dvb/frontends/
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/common/tuners
	cp drivers/media/common/tuners/*.h ${pkgdir}/usr/src/linux-${_kernver}/drivers/media/common/tuners/

	msg "add xfs and shmem for aufs building"
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/fs/xfs
	mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/mm
	cp fs/xfs/xfs_sb.h ${pkgdir}/usr/src/linux-${_kernver}/fs/xfs/xfs_sb.h

	msg "add headers vor virtualbox"
	# in reference to:
	# http://bugs.archlinux.org/task/14568
	cp -a include/drm $pkgdir/usr/src/linux-${_kernver}/include/

	msg "add headers for broadcom wl"
	# in reference to:
	# http://bugs.archlinux.org/task/14568
	cp -a include/trace $pkgdir/usr/src/linux-${_kernver}/include/

	msg "add headers for crypto modules"
	# in reference to:
	# http://bugs.archlinux.org/task/22081
	cp -a include/crypto $pkgdir/usr/src/linux-${_kernver}/include/

	msg "copy in Kconfig files"
	for i in `find . -name "Kconfig*"`; do 
		mkdir -p ${pkgdir}/usr/src/linux-${_kernver}/`echo $i | sed 's|/Kconfig.*||'`
		cp $i ${pkgdir}/usr/src/linux-${_kernver}/$i
	done

	chown -R root.root ${pkgdir}/usr/src/linux-${_kernver}
	find ${pkgdir}/usr/src/linux-${_kernver} -type d -exec chmod 755 {} \;
	
	msg "strip scripts directory"
	find ${pkgdir}/usr/src/linux-${_kernver}/scripts  -type f -perm -u+w 2>/dev/null | while read binary ; do
		case "$(file -bi "$binary")" in
			*application/x-sharedlib*) # Libraries (.so)
				/usr/bin/strip $STRIP_SHARED "$binary";;
			*application/x-archive*) # Libraries (.a)
				/usr/bin/strip $STRIP_STATIC "$binary";;
			*application/x-executable*) # Binaries
				/usr/bin/strip $STRIP_BINARIES "$binary";;
			esac 
	done 

	msg "remove unneeded architectures"
	rm -rf ${pkgdir}/usr/src/linux-${_kernver}/arch/{alpha,arm,arm26,avr32,blackfin,cris,frv,h8300,ia64,m32r,m68k,m68knommu,mips,microblaze,mn10300,parisc,powerpc,ppc,s390,sh,sh64,sparc,sparc64,um,v850,xtensa}
}

package_linux-api-headers() {
	pkgdesc="Kernel headers sanitized for use in userspace"
	url="http://www.gnu.org/software/libc"
	provides=("kernel-headers=${pkgver}"  linux-api-headers=${pkgver})
	#conflicts=('kernel-headers')
	#replaces=('kernel-headers')
	groups=('base' 'CJK')

	cd "$_build_dir"
	make INSTALL_HDR_PATH=${pkgdir}/usr headers_install

	# use headers from libdrm
	rm -rf ${pkgdir}/usr/include/drm
  
	# clean-up unnecessary files generated during install
	find ${pkgdir} -name .install -or -name ..install.cmd | xargs rm -f
}

package_kernel3-tools() {
	pkgdesc="Performance analysis tools for Linux kernel"

	cd "${_build_dir}/tools/perf"
	make DESTDIR="${pkgdir}/usr" install install-man
}

package_kernel3-docs() {
	pkgdesc="Kernel hackers manual - HTML documentation that comes with the Linux kernel."

	cd "$_build_dir"

	mkdir -p $pkgdir/usr/src/linux-$_kernver
	mv Documentation $pkgdir/usr/src/linux-$_kernver
	find $pkgdir -type f -exec chmod 444 {} \;
	find $pkgdir -type d -exec chmod 755 {} \;
	# remove a file already in kernel3 package
	rm -f $pkgdir/usr/src/linux-$_kernver/Documentation/DocBook/Makefile
}
